/*
 * geneticOptParameters.java
 *
 * Created on Sobota, 2007, november 10, 16:58
 */

package geneticOpt;

import java.awt.Color;
import javax.swing.JFrame;
import logic.SimulationParameters;
import logic.schedulers.AlgorithmCollection;
import logic.schedulers.ParametersDialog;

/**
 *
 * @author  alvin
 */
public class geneticOptParameters extends ParametersDialog {
    
    private boolean cyclesCountOK;
    
    
    /** Creates new form geneticOptParameters */
    public geneticOptParameters(JFrame owner) {
        super(owner);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        cyclesCountLbl = new javax.swing.JLabel();
        cyclesCountTxF = new javax.swing.JTextField();
        okBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();

        setTitle("Genetic algorithm parameters");
        setAlwaysOnTop(true);
        cyclesCountLbl.setText("Cycles count");

        cyclesCountTxF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cyclesCountTxFKeyReleased(evt);
            }
        });

        okBtn.setText("OK");
        okBtn.setMaximumSize(new java.awt.Dimension(88, 25));
        okBtn.setMinimumSize(new java.awt.Dimension(88, 25));
        okBtn.setPreferredSize(new java.awt.Dimension(88, 25));
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText("Cancel");
        cancelBtn.setMaximumSize(new java.awt.Dimension(88, 25));
        cancelBtn.setMinimumSize(new java.awt.Dimension(88, 25));
        cancelBtn.setPreferredSize(new java.awt.Dimension(88, 25));
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cyclesCountLbl)
                        .addGap(26, 26, 26)
                        .addComponent(cyclesCountTxF, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cyclesCountLbl)
                    .addComponent(cyclesCountTxF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        int count = Integer.parseInt(cyclesCountTxF.getText());
        SimulationParameters.getLocalScheduler().setParameters(new Integer(count));
        setVisible(false);
    }//GEN-LAST:event_okBtnActionPerformed

    private void cyclesCountTxFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cyclesCountTxFKeyReleased
        try {
            int count = Integer.parseInt(cyclesCountTxF.getText());
            if (count > 0) {
                cyclesCountOK = true;
            } else {
                cyclesCountOK = false;
            }                    
        } catch (NumberFormatException ex3) {
            cyclesCountOK = false;
        }
        
        cyclesCountLbl.setForeground((cyclesCountOK ? okColor : badColor));
        okBtn.setEnabled(cyclesCountOK);
    }//GEN-LAST:event_cyclesCountTxFKeyReleased
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JLabel cyclesCountLbl;
    private javax.swing.JTextField cyclesCountTxF;
    private javax.swing.JButton okBtn;
    // End of variables declaration//GEN-END:variables

    public void setParameters(Object parameters) {
        Integer cyclesCount = (Integer)parameters;
        cyclesCountTxF.setText(cyclesCount.toString());
        
        cyclesCountOK = true;
        cyclesCountLbl.setForeground(okColor);
        okBtn.setEnabled(cyclesCountOK);
    }

}
